//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BondHelper.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Instruments/Bond.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Yield/RateHelper.h>
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (Cephei::QL::IQuote^ cleanPrice, Cephei::QL::Instruments::IBond^ bond) : CRateHelper(CBondHelper::typeid)
{
    CQuote^ _CcleanPrice;
    CBond^ _Cbond;
    try
    {
#ifdef HANDLE
        _phBondHelper = NULL;
#endif
        _CcleanPrice = safe_cast<CQuote^> (cleanPrice);
        _CcleanPrice->Lock();
        Handle<QuantLib::Quote>& _cleanPrice = static_cast<Handle<QuantLib::Quote>&> (_CcleanPrice->GetHandle ()); 
        _Cbond = safe_cast<CBond^> (bond);
        _Cbond->Lock();
        boost::shared_ptr<QuantLib::Bond>& _bond = static_cast<boost::shared_ptr<QuantLib::Bond>&> (_Cbond->GetShared ()); 
        _ppBondHelper = new boost::shared_ptr<QuantLib::BondHelper> (new QuantLib::BondHelper ( _cleanPrice,  _bond ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcleanPrice != nullptr) _CcleanPrice->Unlock();
        if (_Cbond != nullptr) _Cbond->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (boost::shared_ptr<QuantLib::BondHelper>& childNative, Object^ owner) : CRateHelper(CBondHelper::typeid)
{
#ifdef HANDLE
	_phBondHelper = NULL;
#endif
	_ppBondHelper = &childNative;
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
}
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (QuantLib::BondHelper& childNative, Object^ owner) : CRateHelper(CBondHelper::typeid)
{
#ifdef HANDLE
	_phBondHelper = NULL;
#endif
	_ppBondHelper = new boost::shared_ptr<QuantLib::BondHelper> (&childNative);
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
    _BondHelperOwner = owner;
    _RateHelperOwner = owner;
}

Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (CBondHelper^ copy) : CRateHelper(CBondHelper::typeid)
{
#ifdef HANDLE
	_phBondHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBondHelper = new boost::shared_ptr<QuantLib::BondHelper> (copy->GetShared());
        _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
    }
}
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (PLATFORM::Type^ t) : CRateHelper(CBondHelper::typeid)
{
#ifdef HANDLE
	_phBondHelper = NULL;
#endif
	if (!t->IsSubclassOf(CBondHelper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (QuantLib::Handle<QuantLib::BondHelper>& childNative, Object^ owner)  : CRateHelper(CBondHelper::typeid)
{
	_phBondHelper = &childNative;
	_ppBondHelper = &static_cast<boost::shared_ptr<QuantLib::BondHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
    _BondHelperOwner = owner;
}
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (QuantLib::Handle<QuantLib::BondHelper> childNative)  : CRateHelper(CBondHelper::typeid)
{
	_phBondHelper = &childNative;
	_ppBondHelper = &static_cast<boost::shared_ptr<QuantLib::BondHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CBondHelper::CBondHelper (QuantLib::BondHelper childNative)  : CRateHelper(CBondHelper::typeid)
{
#ifdef HANDLE
	_phBondHelper = NULL;
#endif
	_ppBondHelper = new boost::shared_ptr<QuantLib::BondHelper> (new QuantLib::BondHelper (childNative));
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppBondHelper));
}
#endif

Cephei::QL::Termstructures::Yield::CBondHelper::~CBondHelper ()
{
    if (_ppBondHelper != NULL)
    {
	    delete _ppBondHelper;
        _ppBondHelper = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CBondHelper::!CBondHelper ()
{
    if (_ppBondHelper != NULL)
    {
	    delete _ppBondHelper;
    }
}
QuantLib::BondHelper& Cephei::QL::Termstructures::Yield::CBondHelper::GetReference ()
{
    if (_ppBondHelper == NULL) throw REFNEW NativeNullException ();
	return **_ppBondHelper;
}
boost::shared_ptr<QuantLib::BondHelper>& Cephei::QL::Termstructures::Yield::CBondHelper::GetShared ()
{
    if (_ppBondHelper == NULL) throw REFNEW NativeNullException ();
	return *_ppBondHelper;
}
QuantLib::BondHelper* Cephei::QL::Termstructures::Yield::CBondHelper::GetPointer ()
{
    if (_ppBondHelper == NULL) throw REFNEW NativeNullException ();
	return &**_ppBondHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BondHelper>& Cephei::QL::Termstructures::Yield::CBondHelper::GetHandle ()
{
	if (_phBondHelper == NULL)
	{
		_phBondHelper = new Handle<QuantLib::BondHelper> (*_ppBondHelper);
	}
	return *_phBondHelper;
}
#endif
bool Cephei::QL::Termstructures::Yield::CBondHelper::HasNative () 
{
	return (_ppBondHelper != NULL);
}

Cephei::QL::Instruments::IBond^ Cephei::QL::Termstructures::Yield::CBondHelper::Bond::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::Bond> _rv = (boost::shared_ptr<QuantLib::Bond>)(*_ppBondHelper)->bond ( );   
        Cephei::QL::Instruments::CBond^ _nrv = REFNEW Cephei::QL::Instruments::CBond (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Yield::CBondHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBondHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Yield::IBondHelper^ Cephei::QL::Termstructures::Yield::CBondHelper::SetTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ t)
{
    CYieldTermStructure^ _Ct;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ct = safe_cast<CYieldTermStructure^> (t);
        _Ct->Lock();
        QuantLib::YieldTermStructure* _t = static_cast<QuantLib::YieldTermStructure*> (_Ct->GetPointer ()); 
    	(*_ppBondHelper)->setTermStructure ( _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ct != nullptr) _Ct->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CBondHelperVector::CBondHelperVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Termstructures::Yield::CBondHelperVector::~CBondHelperVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Termstructures::Yield::CBondHelperVector::!CBondHelperVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CBondHelperVector::CBondHelperVector (std::vector<QuantLib::BondHelper>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Termstructures::Yield::CBondHelperVector::CBondHelperVector (std::vector<QuantLib::BondHelper> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::BondHelper> (vec);
}
#endif
Cephei::QL::Termstructures::Yield::CBondHelperVector::CBondHelperVector (std::vector<boost::shared_ptr<QuantLib::BondHelper>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CBondHelperVector::CBondHelperVector (std::vector<QuantLib::Handle<QuantLib::BondHelper>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Termstructures::Yield::CBondHelperVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Termstructures::Yield::IBondHelper^ Cephei::QL::Termstructures::Yield::CBondHelperVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw REFNEW RangeException ();
		QuantLib::BondHelper& native = (*_pVValue)[index];
		return REFNEW CBondHelper (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw REFNEW RangeException ();
		QuantLib::BondHelper& native = *(*_pVBoost)[index];
		return REFNEW CBondHelper (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw REFNEW RangeException ();
		QuantLib::BondHelper& native = *((*_pVHand)[index].currentLink ());
		return REFNEW CBondHelper (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Termstructures::Yield::CBondHelperVector::default::set (int index, Cephei::QL::Termstructures::Yield::IBondHelper^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CBondHelper^>(value)->GetReference ());
		}
        else
        {
            QuantLib::BondHelper& c = (*_pVValue)[index];
            c = dynamic_cast<CBondHelper^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CBondHelper^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::BondHelper>& c = (*_pVBoost)[index];
		c = dynamic_cast<CBondHelper^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::BondHelper> ());
		}
		QuantLib::Handle<QuantLib::BondHelper>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::BondHelper>& b = static_cast<boost::shared_ptr<QuantLib::BondHelper>>(c.currentLink());
		b = dynamic_cast<CBondHelper^>(value)->GetShared ();
	}
#endif
}
Cephei::Core::Generic::ICoCell<Cephei::QL::Termstructures::Yield::IBondHelper^>^ Cephei::QL::Termstructures::Yield::CBondHelperVector::CItem::get (int index) 
{
	return CellFactory::Instance->Create<IBondHelper^> (dynamic_cast<IBondHelper^>(default[index]));
}

void Cephei::QL::Termstructures::Yield::CBondHelperVector::CItem::set (int index, Cephei::Core::Generic::ICoCell<Cephei::QL::Termstructures::Yield::IBondHelper^>^ value) 
{
	default[index] = safe_cast<IBondHelper^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::BondHelper>& Cephei::QL::Termstructures::Yield::CBondHelperVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::BondHelper>>& Cephei::QL::Termstructures::Yield::CBondHelperVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::BondHelper>>& Cephei::QL::Termstructures::Yield::CBondHelperVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Termstructures::Yield::CBondHelperVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::BondHelper> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::BondHelper>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::BondHelper>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IBondHelper^ Cephei::QL::Termstructures::Yield::CBondHelper_Factory::Create (Cephei::QL::IQuote^ cleanPrice, Cephei::QL::Instruments::IBond^ bond)
{
    return REFNEW CBondHelper ( cleanPrice,  bond);
}
/* <summary>Create a single dimension array of BondHelper that can be passed into other function </summary> */
IVector<Cephei::QL::Termstructures::Yield::IBondHelper^>^ Cephei::QL::Termstructures::Yield::CBondHelper_Factory::CreateVector ()
 {
	 return REFNEW CoVector<IBondHelper^> (REFNEW CBondHelperVector ());
 }
IVector<Cephei::QL::Termstructures::Yield::IBondHelper^>^ Cephei::QL::Termstructures::Yield::CBondHelper_Factory::CreateVector (PLATFORM::Collections::Generic::IEnumerable<IBondHelper^>^ source)
 {
	 return REFNEW CoVector<IBondHelper^> (source, REFNEW CBondHelperVector ());
 }
/* <summary>Create a single dimension array of BondHelper with change events</summary> */
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Termstructures::Yield::IBondHelper^>^>^>^ Cephei::QL::Termstructures::Yield::CBondHelper_Factory::CreateCellVector ()
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Termstructures::Yield::IBondHelper^>^>^> (REFNEW CoCellVector<Cephei::QL::Termstructures::Yield::IBondHelper^> (REFNEW CBondHelperVector ()));
 }
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Termstructures::Yield::IBondHelper^>^>^>^ Cephei::QL::Termstructures::Yield::CBondHelper_Factory::CreateCellVector (PLATFORM::Collections::Generic::IEnumerable<Cephei::Core::Generic::ICoCell<Cephei::QL::Termstructures::Yield::IBondHelper^>^>^ source)
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Termstructures::Yield::IBondHelper^>^>^> (REFNEW CoCellVector<Cephei::QL::Termstructures::Yield::IBondHelper^> (source, REFNEW CBondHelperVector ()));
 }

